﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;

namespace Common.Script.Npc
{
    public class NPCScriptManager : AbstractScriptManager
    {
        /// <summary>
        /// 存放NPC函数字典(根据NPCid来决定)
        /// </summary>
        private Dictionary<MapleClient, NPCConversationManager> cms = new Dictionary<MapleClient, NPCConversationManager>();

        /// <summary>
        /// NPC管理实例
        /// </summary>
        private static NPCScriptManager instance = new NPCScriptManager();

        public static NPCScriptManager getInstance()
        {
            if (instance == null)
            {
                instance = new NPCScriptManager();
            }
            return instance;
        }


        /// <summary>
        /// 开始脚本
        /// </summary>
        /// <param name="npcid"></param>
        public void Start(MapleClient c,int Npcid)
        {
            try
            {
                Common.LuaEngine.MapleLuaEngine iv = getInvocable("\\Npc\\" + Npcid + ".lua");
                //创建脚本函数库
                NPCConversationManager cm = new NPCConversationManager(c,Npcid,iv);
                //把函数库注册进Lua脚本
                iv.BindLuaApiClass(cm);
                //执行脚本
                iv.Invoking("Start");

                cms.Add(c, cm);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("找不到Lua脚本:" + Npcid + ".lua"+"\r\n 错误:"+e);
            }
        }


        /// <summary>
        /// 动作
        /// </summary>
        /// <param name="Npcid"></param>
        /// <param name="mode"></param>
        /// <param name="type"></param>
        /// <param name="selection"></param>
        public void Action(MapleClient c,int Npcid,byte mode, byte type, int selection)
        {
            NPCConversationManager _NpcConversation = cms[c];

            if(_NpcConversation != null)
            {
                _NpcConversation.GetEngine().Invoking("Action",mode,type,selection);
            }
            else
            {
                System.Console.WriteLine("执行脚本出错:"+Npcid);
            }
        }

        /// <summary>
        /// 加载全部NPC类脚本
        /// </summary>
        public NPCScriptManager()
        {

        }
    }
}
